package com.xh.ssm.controller;

import com.xh.ssm.bean.Project;
import com.xh.ssm.service.ProjectService;
import com.xh.ssm.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Author PangJunjie
 * @Date 2024/1/29/029
 */
@Controller
@RequestMapping("/project")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @GetMapping("/list")
    public String list() {
        return "project/project-list";
    }

    @GetMapping("/page")
    @ResponseBody
    public R getPage(int page, int limit, Project project) {
        return projectService.getPage(page, limit, project);
    }

    @GetMapping("/edit")
    public String edit(Integer id, Model model) {
        if (id != null) {
            model.addAttribute("project", projectService.getById(id));
        }
        return "project/project-update";
    }

    @PostMapping(value = {"/update", "/status"})
    @ResponseBody
    public R update(Project project) {
        projectService.saveOrUpdate(project);
        return R.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public R delete(@PathVariable int id) {
        projectService.deleteById(id);
        return R.ok();
    }

    @GetMapping("/select")
    @ResponseBody
    public R select() {
        return projectService.getList();
    }

}
